package cn.wolfcode.luowowo.mgrsite.job;

import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.vo.StatisVo;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVoService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVo;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 将redis中数据定时持久化到mysql中
 *
 * redis的持久化操作
 */
@Component
public class RedisDataPersistenceJob {


    @Reference
    private IStrategyStatisVoService strategyStatisVoService;

    @Reference
    private IStrategyDetailService strategyDetailService;

    //Scheduled定时任务标签
    //cron:定时任务计划,指定定时任务的周期
    @Scheduled(cron = "0/10 * * * * ?")
    public void doWork(){
        System.out.println("----------------redis持久化--开始-------------");
        //获取所有的vo对象
        List<StrategyStatisVo> strategyStatisVoList =  strategyStatisVoService.listStrategyStatisVo(RedisKeys.STRATEGY_STATIS_VO.jion("*"));
        //将所有vo对象持久化到mysql中
        for (StrategyStatisVo strategyStatisVo : strategyStatisVoList) {
            StatisVo statisVo = new StatisVo();
            BeanUtils.copyProperties(strategyStatisVo,statisVo);
            strategyDetailService.updateStrategyStatisVo(statisVo);
        }
        System.out.println("----------------redis持久化--结束-------------");
    }
}
